#include "stdint.h"
#include "../memory.h"
#include "../render.h"
#include "tail.h"


may_tail_view_t *tail_view_create(void *parent)
{
    widget_t *parent_w = (widget_t *)parent;
    if (parent_w->widget_type != widget_type_screen)
    {
        return NULL;
    }

    may_tail_view_t *tail_view = (may_tail_view_t *)memory_alloc(sizeof(may_tail_view_t));
    tail_view->obj.widget_type = widget_type_tail_view;
    tail_view->obj.x = 0;
    tail_view->obj.y = 0;
    tail_view->obj.h = parent_w->h;
    tail_view->obj.w = parent_w->w;

    list_add_tail(&tail_view->obj.node, &((widget_t *)parent)->node);
    list_init(&tail_view->sub_node);

    return tail_view;
}

void tail_view_add_widget(void *parent, widget_t *widget)
{
    widget_t *parent_w = (widget_t *)parent;
    if (parent_w->widget_type != widget_type_tail_view)
    {
        return;
    }
    list_add_tail(&widget->node, &((may_tail_view_t *)parent)->sub_node);
}

void tail_view_remove(may_tail_view_t *tail_view)
{
    list_del(&tail_view->obj.node);
    memory_free(tail_view);
}

void tail_view_render(void *obj)
{
    may_tail_view_t* tail_view = (may_tail_view_t*)obj;
    list_node_t *node = NULL;
    list_node_t *n = NULL;
    list_for_each(node, &tail_view->sub_node)
    {
        widget_t *widget = list_entry(node, widget_t, node);
        render_widget(widget);
    }

}